package com.example.toutiao.base.impl;

import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.toutiao.MainActivity;
import com.example.toutiao.R;
import com.example.toutiao.base.BaseFragment;
import com.example.toutiao.base.BasePager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;

/**
 * Created by admin on 2017/11/14.
 */

public class ContentFragment extends BaseFragment {

    private ViewPager mViewPager;

    private RadioGroup rgGroup;

    private ArrayList<BasePager> mPagers;

    @Override
    public View initView() {
        View view = View.inflate(myActivity, R.layout.fragment_content, null);

        changeRadioImageSize(view);

        mViewPager = (ViewPager)view.findViewById(R.id.vp_content);
        rgGroup = (RadioGroup)view.findViewById(R.id.rg_group);

        return view;
    }

    @Override
    public void initData() {
        //初始化四个标签页面
        mPagers = new ArrayList<BasePager>(4);
        mPagers.add(new HomePager(myActivity));
        mPagers.add(new XiguaVideoPager(myActivity));
        mPagers.add(new SmallHeadNewsPager(myActivity));
        mPagers.add(new SmallVideoPager(myActivity));

        mViewPager.setAdapter(new ContentAdapter());

        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_home :
                        //首页
                        mViewPager.setCurrentItem(0, false);    //禁用页面的切换动画效果
                        mPagers.get(0).initData();      //切到当前页面再初始化数据
                        setSlidingMenuEnable(false);   //禁用侧边栏滑动效果
                        break;
                    case R.id.rb_xiguaVideo :
                        //西瓜视频
                        mViewPager.setCurrentItem(1, false);
                        mPagers.get(1).initData();      //切到当前页面再初始化数据
                        setSlidingMenuEnable(true);   //开启侧边栏滑动效果
                        break;
                    case R.id.rb_smallHeadNews:
                        //微头条
                        mViewPager.setCurrentItem(2, false);
                        mPagers.get(2).initData();      //切到当前页面再初始化数据
                        setSlidingMenuEnable(true);   //开启侧边栏滑动效果
                        break;
                    case R.id.rb_smallVideo :
                        //小视频
                        mViewPager.setCurrentItem(3, false);
                        mPagers.get(3).initData();      //切到当前页面再初始化数据
                        setSlidingMenuEnable(true);   //开启侧边栏滑动效果
                        break;
                    default:
                        break;
                }
            }
        });

        mPagers.get(0).initData();  //第一次进来，初始化首页数据
        setSlidingMenuEnable(false);   //禁用侧边栏滑动效果
    }

    class ContentAdapter extends PagerAdapter{
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = mPagers.get(position);
            container.addView(pager.mRootView);     //将页面布局添加到容器中
//            pager.initData();   //初始化数据,次数尽量不要初始化，只有切到当前页面，才初始化数据，节省流量和性能
            return pager.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView((View)object);
        }

        @Override
        public int getCount() {
            return mPagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

    /**
     * 设置侧边栏是否可用
     * @param enable
     */
    private void setSlidingMenuEnable(boolean enable){
        MainActivity mainUI = (MainActivity)myActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();

        if(enable){
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }else{
            //禁用掉侧边栏滑动效果
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }

    /**
     * 修改底部按钮图片大小
     */
    public void changeRadioImageSize(View view){
        RadioButton rbHome = (RadioButton)view.findViewById(R.id.rb_home);
        RadioButton rbXiguaVideo = (RadioButton)view.findViewById(R.id.rb_xiguaVideo);
        RadioButton rbSmallHeadNews = (RadioButton)view.findViewById(R.id.rb_smallHeadNews);
        RadioButton rbSmallVideo = (RadioButton)view.findViewById(R.id.rb_smallVideo);

        Drawable drawable0 = getResources().getDrawable(R.drawable.btn_home_selector);
        Drawable drawable1 = getResources().getDrawable(R.drawable.btn_xiguavideo_selector);
        Drawable drawable2 = getResources().getDrawable(R.drawable.btn_smallheadnews_selector);
        Drawable drawable3 = getResources().getDrawable(R.drawable.btn_smallvideo_selector);

        drawable0.setBounds(0, 0, 100, 100);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        drawable1.setBounds(0, 0, 100, 100);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        drawable2.setBounds(0, 0, 100, 100);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        drawable3.setBounds(0, 0, 100, 100);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度

        rbHome.setCompoundDrawables(null, drawable0, null, null);//只放上面
        rbXiguaVideo.setCompoundDrawables(null, drawable1, null, null);//只放上面
        rbSmallHeadNews.setCompoundDrawables(null, drawable2, null, null);//只放上面
        rbSmallVideo.setCompoundDrawables(null, drawable3, null, null);//只放上面
    }



}












